﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using ThePingMasterConfiguration.Services;
using ThePingMasterConfiguration.ViewModels;

namespace ThePingMasterConfiguration.Commands
{
    public class SaveCommand : ICommand
    {
        private readonly ConfigurationModel _model;
        private readonly IMessageService _service;

        public SaveCommand(ConfigurationModel model, IMessageService service)
        {
            if (model == null) throw new ArgumentNullException("model");
            if (service == null) throw new ArgumentNullException("service");
            _model = model;
            _service = service;
        }

        public void Execute(object parameter)
        {
            try
            {
                _model.Save();
                _service.ShowMessage("Configuration successfully saved.\nRestart the service for your changes to take effect.");
            }
            catch (Exception)
            {
                _service.ShowMessage("Unable to save new configuration");
            }            
        }

        public bool CanExecute(object parameter)
        {
            return _model.Validate();
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}
